package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.inStorage.OtherSelectSupplierAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageOtherSelectSupplierBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageOtherSelectSupplierRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;


//验收入库
public class InStorageOtherSelectSupplierActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView supplierList;

    private OtherSelectSupplierAdapter adapter;

    private List<InStorageOtherSelectSupplierBean.DataBean.SupplierListBean> allData;

    private String mSupplierCode;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_other_select_supplier);
        initView(InStorageOtherSelectSupplierActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(InStorageOtherSelectSupplierActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        supplierList = (PullableListView) activity.findViewById(R.id.supplierList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.supplierListRefresh);
    }

    private void initData(Activity activity) {
        title.setText("查询供应商");
        ActivityUtils.initBackAndFunction(activity, "其他入库", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        supplierList.setPullUpFlag(false);
        supplierList.setPullDownFlag(false);
        mSupplierCode = "";
        addSupplierListData(activity, new ArrayList<>(), supplierList, true);
//        setmScanListener((String scanResult, int length) -> {
//            getData(activity, QRCodeUtils.DecryptQRCode(scanResult, QRCodeConstant.PARAMETER_SUPPLIER_NAME));
//        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE, QRCodeConstant.TYPE_PURCHASE_ORDER, QRCodeConstant.TYPE_POSITION);
    }

    private void addSupplierListData(Activity activity, List<InStorageOtherSelectSupplierBean.DataBean.SupplierListBean> supplierList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new OtherSelectSupplierAdapter(activity, supplierList, mSupplierCode, new InStorageOtherSelectSupplierListener() {
                @Override
                public NoDoubleClickListener getNoDoubleClickListener(EditText supplierCode) {
                    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            if (TextUtils.isEmpty(supplierCode.getText().toString())) {
                                Utils.middleToast(activity, "供应商代码不可以为空");
                            } else {
                                Utils.closeSoftKeyboard(activity, v);
                                mSupplierCode = supplierCode.getText().toString();
                                getData(activity, mSupplierCode);
                            }
                        }
                    };
                    return noDoubleClickListener;
                }

                @Override
                public NoDoubleClickListener select(String supplierCode, String supplierDescription) {
                    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                        @Override
                        public void onNoDoubleClick(View v) {
                            Intent intent = new Intent();
                            intent.putExtra(InStorageExtra.CODE, supplierCode);
                            intent.putExtra(InStorageExtra.DESCRIPTION, supplierDescription);
                            activity.setResult(InStorageOtherIndexActivity.SELECT_SUPPLIER, intent);
                            activity.finish();
                        }
                    };
                    return noDoubleClickListener;
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Activity activity, String condition) {
        InStorageOtherSelectSupplierRequest.request(activity, condition, new GsonBeanListener<InStorageOtherSelectSupplierBean>() {
            @Override
            public void onSuccess(InStorageOtherSelectSupplierBean bean) {
                allData = bean.getData().getSupplierList();
                if (allData == null || allData.isEmpty()) {
                    Utils.middleToast(activity, "未查到数据");
                }
                mSupplierCode = condition;
                addSupplierListData(activity, allData, supplierList, true);
            }
        });
    }

    public interface InStorageOtherSelectSupplierListener {
        NoDoubleClickListener getNoDoubleClickListener(EditText supplierCode);

        NoDoubleClickListener select(String supplierCode, String supplierDescription);
    }

}
